package com.qst.itoffer.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import com.qst.itoffer.bean.ApplicantBean;
import com.qst.itoffer.util.DBUtil;

public class ApplicantDao {
	
	/**
	 * 验证邮箱的唯一性：注册使用的邮箱不能重复
	 * @param email
	 * @return
	 */
	
	public boolean isExistEmail(String email) {
		Connection cont = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet res = null;
		
		String sql = "select *from tb_applicant where APPLICANT_EMAIL = ?";
		try {
			pstmt = cont.prepareStatement(sql);
			pstmt.setString(1, email);
			res = pstmt.executeQuery();
			if(res.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.closeJDBC(cont, pstmt, res);
		}
		return false;
	}
	
	/**
	 * 求职者注册信息保存
	 * @param email
	 * @param pwad
	 */
	public void insertEmail(String email,String pawd) {
		Connection cont = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet res = null;
		
		String sql="insert into tb_applicant(APPLICANT_EMAIL,APPLICANT_PWD,APPLICANT_REGISTDATE) values(?,?,?)";
		try {
			pstmt = cont.prepareStatement(sql);
			pstmt.setString(1, email);
			pstmt.setString(2, pawd);
			pstmt.setTimestamp(3, new Timestamp(new Date().getTime()));
			pstmt.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.closeJDBC(cont, pstmt, res);
		}
		
	}
	
	/**
	 * 验证登录的方法
	 * @param email
	 * @return
	 */
	
	public ApplicantBean valiLogin(String email) {
		Connection cont = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet res = null;
		ApplicantBean applicantBean = new ApplicantBean();
		
		String sql = "select *from tb_applicant where APPLICANT_EMAIL = ?";
		
		try {
			pstmt = cont.prepareStatement(sql);
			pstmt.setString(1, email);
			res = pstmt.executeQuery();
			if(res.next()) {
				applicantBean.setApplicant_id(res.getInt(1));
				applicantBean.setApplicant_email(res.getString("APPLICANT_EMAIL"));
				applicantBean.setApplicant_pawd(res.getString("APPLICANT_PWD"));
				applicantBean.setApplicant_date(res.getDate("APPLICANT_REGISTDATE"));


			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.closeJDBC(cont, pstmt, res);
		}
		return applicantBean;
		
	}
	
	/**
	 * 登陆成功后，判断是否有简历
	 * @param applicantId
	 * @return
	 */
	
	public int isExistResume(int applicantId) {
		Connection cont = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet res = null;
		int resumeId=0;
		String sql = "select *from tb_resume_basicinfo where APPLICANT_ID = ?";
		
		try {
			pstmt = cont.prepareStatement(sql);
			pstmt.setInt(1, applicantId);
			res = pstmt.executeQuery();
			if(res.next()) {
				resumeId=res.getInt("BASICINFO_ID");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtil.closeJDBC(cont, pstmt, res);
		}
		return resumeId;
		
	}
}
